package cn.sportvip.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sportvip.dto.user.UserInfoDto;
import cn.sportvip.entity.user.Credential;
import cn.sportvip.entity.user.Feedback;
import cn.sportvip.entity.user.UserInfo;
import cn.sportvip.service.user.UserService;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;

	/**注册*/
	@RequestMapping("register")
	@ResponseBody
	public UserInfoDto register(@RequestParam String phone, String email,@RequestParam String password, UserInfo userInfo
			) {
		Credential credential = new Credential();
		credential.setEmail(email);
		credential.setPassword(password);
		credential.setPhone(phone);
		userService.register(credential, userInfo);
		return new UserInfoDto(userInfo, credential);
	}
	
	/**登陆*/
	@RequestMapping("login")
	@ResponseBody
	public UserInfoDto login(@RequestParam String emailOrPhone, @RequestParam String password) {
		return userService.login(emailOrPhone, password);
	}
	/**更新用户资料*/
	@RequestMapping("updateUserInfo")
	@ResponseBody
	public UserInfo updateUserInfo(UserInfo userInfo) {
		return userService.updateUserInfo(userInfo);
	}
	/**更新用户登陆信息*/
	@RequestMapping("updateCredential")
	@ResponseBody
	public Credential updateCredential(Credential credential, String emailOrPhone, String oldPassword) {
		return userService.updateCredentail(credential, emailOrPhone, oldPassword);
	}
	/**找回密码*/
	@RequestMapping("forgotPassword")
	@ResponseBody
	public JSONObject forgotPassword(String email) {
		return userService.forgotPassword(email);
	}
	/**到找回密码页面密码*/
	@RequestMapping("gotoResetPassword/{email}/{uuid}")
	public String gotoResetPassword(@PathVariable String email, @PathVariable String uuid) {
		boolean success = userService.validateResetPasswordRequest(email, uuid);
		if(success){
			return "resetpassword_input";
		} else {
			return "resetpassword_fail";	
		}
	}
	/**邮件重置密码*/
	@RequestMapping("resetPassword")
	public String emailResetPassword(@RequestParam String email, @RequestParam String uuid, String newPassword) {
		boolean succ = userService.emailResetPassword(email, uuid, newPassword);
		if(succ) {
			return "resetpassword_success";
		} else {
			return "resetpassword_fail";
		}
	}
	/**反馈*/
	@RequestMapping("feedback")
	@ResponseBody
	public Feedback feedback(Feedback feedback) {
		return userService.addFeedback(feedback);
	}
}
